{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "import csv\n",
    "import datetime\n",
    "import numpy.random\n",
    "import random"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. confidence为信度，confidence越高代表本条数据越可信，仅保留0.9及以上confidence的数据 其他行数据丢弃，输出剩余数据条数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "87442\n"
     ]
    }
   ],
   "source": [
    "# 获取文件路径\n",
    "file = './gaze.csv'\n",
    "\n",
    "data = open(file,mode='r',encoding='utf-8')\n",
    "lst = []\n",
    "reader = csv.reader(data)\n",
    "for i in reader:\n",
    "    if i[2][2]=='9':\n",
    "        lst.append(i)\n",
    "data.close()\n",
    "print(len(lst))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 列norm_pos_x和norm_pos_y为预测的归一化后的坐标值，假设norm_pos_x和norm_pos_y符合正态分布，利用3 sigma原则去除无效值 输出剩余数据条数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120036\n"
     ]
    }
   ],
   "source": [
    "# 得到mean_pos_x mean_pos_y\n",
    "data = open(file,mode='r',encoding='utf-8')\n",
    "pos_x = []\n",
    "pos_y = []\n",
    "reader = csv.reader(data)\n",
    "for i in reader:\n",
    "    if i[3][5].isnumeric():\n",
    "        pos_x.append(float(i[3]))\n",
    "        pos_y.append(float(i[4]))\n",
    "mean_pos_x = sum(pos_x) / len(pos_x)\n",
    "mean_pos_y = sum(pos_y) / len(pos_y)\n",
    "\n",
    "sigma_x = 0\n",
    "for i in pos_x:\n",
    "    sigma_x += (i -mean_pos_x) ** 2\n",
    "sigma_x  = (sigma_x/len(pos_x))** (1/2)\n",
    "sigma_y = 0\n",
    "for i in pos_y:\n",
    "    sigma_y += (i -mean_pos_y) ** 2\n",
    "sigma_y  = (sigma_y/len(pos_y))** (1/2)\n",
    "\n",
    "data = open(file,mode='r',encoding='utf-8')\n",
    "reader = csv.reader(data)\n",
    "lst_x = []\n",
    "lst_y = []\n",
    "for i in reader:\n",
    "    if i[3][4].isnumeric() and float(i[3]) < mean_pos_x + 3* sigma_x and float(i[3]) > mean_pos_x - 3 * sigma_x and i[4][4].isnumeric() and float(i[4]) < mean_pos_y + 3* sigma_y and float(i[4]) > mean_pos_y - 3 * sigma_y:\n",
    "        lst_x.append(i)\n",
    "        lst_y.append(i)\n",
    "print(len(lst_x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. gaze_timestamp列是时间戳（单位为s），为了方便分析人员查看，将其改为\"1970-01-02T17:44:30.225707+0000\"的形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1970-01-02 08:29:10.776780\n",
      "1970-01-02 08:29:10.776787\n",
      "1970-01-02 08:29:10.779709\n",
      "1970-01-02 08:29:10.779759\n",
      "1970-01-02 08:29:10.787485\n",
      "1970-01-02 08:29:10.787580\n",
      "1970-01-02 08:29:10.789480\n",
      "1970-01-02 08:29:10.793583\n",
      "1970-01-02 08:29:10.795683\n",
      "1970-01-02 08:29:10.798406\n"
     ]
    }
   ],
   "source": [
    "timelist = []\n",
    "data = open(file,mode='r',encoding='utf-8')\n",
    "reader = csv.reader(data)\n",
    "for i in reader:\n",
    "    if i[0][4].isnumeric():\n",
    "        timee = datetime.datetime.fromtimestamp(float(i[0]))\n",
    "        timelist.append(timee.strftime(\"%Y-%m-%d %H:%M:%S.%f\"))\n",
    "for i in range(10):\n",
    "    print (timelist[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. 高帧率采集设备在采集每帧时有一定的偏差，请问该数据的采样率是多少？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "427.6261076608263\n"
     ]
    }
   ],
   "source": [
    "d1 = datetime.datetime.strptime(timelist[0], '%Y-%m-%d %H:%M:%S.%f')\n",
    "d2 = datetime.datetime.strptime(timelist[-1], '%Y-%m-%d %H:%M:%S.%f')\n",
    "# 时间间隔\n",
    "interval = float(str((d2-d1).seconds) + '.'+str((d2-d1).microseconds))\n",
    "data = open(file,mode='r',encoding='utf-8')\n",
    "reader = csv.reader(data)\n",
    "count = 0\n",
    "for i in reader:\n",
    "    if i[0][4].isnumeric():\n",
    "        count += 1\n",
    "frequency = count / interval\n",
    "print(frequency)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. 高帧率采集设备在采集每帧的时候需要打上时间戳，但是时间戳有误差，每一帧时间戳不是严格按照采样率，为了后期时序数据处理方便，将gaze_timestamp列按照100Hz的帧重新采样，输出处理后的数据集的前二十行数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88150.02512\n",
      "88150.02569\n",
      "88150.02631\n",
      "88150.02764\n",
      "88150.02956\n",
      "88150.05085\n",
      "88150.05820\n",
      "88150.05893\n",
      "88150.05908\n",
      "88150.09435\n",
      "88150.09842\n",
      "88150.10056\n",
      "88150.13177\n",
      "88150.13340\n",
      "88150.13624\n",
      "88150.14981\n",
      "88150.16331\n",
      "88150.17049\n",
      "88150.18727\n",
      "88150.19076\n"
     ]
    }
   ],
   "source": [
    "\n",
    "data = open(file,mode='r',encoding='utf-8')\n",
    "lst = []\n",
    "reader = csv.reader(data)\n",
    "\n",
    "\n",
    "for i in reader:\n",
    "    if i[0][4].isnumeric():\n",
    "        lst.append(i)\n",
    "inittime = lst[0][0][:5]\n",
    "new_lst = numpy.random.permutation(lst)\n",
    "data.close()\n",
    "\n",
    "# 打乱顺序后的list\n",
    "# 为体现打乱重采样，保留源数据集合中world_index\n",
    "\n",
    "random_lst = []\n",
    "\n",
    "def ran(n):\n",
    "# 生成n个不同的随机数\n",
    "    for i in range(n):\n",
    "        rannum = random.random()\n",
    "        while rannum in random_lst:\n",
    "            rannum = random.random()\n",
    "        rannum = str(rannum)[1:7]\n",
    "        random_lst.append(rannum)\n",
    "    random_lst.sort()\n",
    "    return random_lst\n",
    "\n",
    "# 模拟100Hz重采样\n",
    "for i in range(1,1254):\n",
    "    if i != 1253:\n",
    "        ranlst = ran(100)\n",
    "        for j in range(1,100):\n",
    "            new_lst[(i - 1) * 100 +j-1][0] =  inittime+ranlst[j]\n",
    "        inittime = str(int(inittime)+1)\n",
    "    if i == 1253:\n",
    "        ranlst = ran(64)\n",
    "        for j in range(1,64):\n",
    "            new_lst[(i - 1) * 100 +j-1][0] =  inittime+ranlst[j]\n",
    "for i in range(20):\n",
    "    print(new_lst[i][0])\n",
    "    # 为输出显示方便 仅输出了前二十条采样的时间戳"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "MindSpore(1.1.1)",
   "language": "python",
   "name": "mindspore"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
